package com.bw.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.service.GmallService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
public class GmallController {
    //注入业务层
    @Autowired
    GmallService gmallService;

    //4：根据存储过程总成交金额的
    @RequestMapping("/findTotal")
    public Object findTotal(String nowDate){
        //1. 拿到数据库中的数据
        List<Map> list = gmallService.findTotal("2024-04-22");
        //2. 返回大屏所需要的数据
        //2.1 实例化json对象
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        jsonObject.put("data",list.get(0).get("total"));
        return jsonObject;
    }

    //2：按品牌展示销售排行
    @RequestMapping("/findTradTotal")
    public Object findTradTotal(String nowDate){
        //1. 拿到数据库中的数据
        List<Map> list = gmallService.findTradTotal("2024-04-21");
        //2. 返回大屏所需要的数据
        //2.1 实例化json对象
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        //2.2 创建dataJSON对象
        JSONObject dataObject = new JSONObject();
        //2.3 需要categories的数组 x轴
        JSONArray cateArray = new JSONArray();
        //x轴放入数据库查到的值 遍历数据库中的值
        for (Map map : list) {
            //数组里面添加品牌名
            cateArray.add(map.get("name"));
        }
        dataObject.put("categories",cateArray);
        //2.4 需要series的数组 y轴
        JSONArray serArray = new JSONArray();
        //2.5 数组需要做一个集合
        JSONObject nameObject = new JSONObject();
        //2.6 集合中需要放 name ，data
        nameObject.put("name","name");
        JSONArray dataArray = new JSONArray();
        for (Map map : list) {
            dataArray.add(map.get("total"));
        }
        nameObject.put("data",dataArray);
        //2.7 series 的数组添加 nameObject
        serArray.add(nameObject);

        dataObject.put("series",serArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    }

    //1：按照省份展示总成交额指标
    @RequestMapping("/findProTotal")
    public Object findProTotal(String nowDate){
        List<Map> list = gmallService.findProTotal("2024-04-21");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray mapArray = new JSONArray();
        for (Map map : list) {
            JSONObject mapObject = new JSONObject();
            mapObject.put("name",map.get("name"));
            mapObject.put("value",map.get("total"));
            mapArray.add(mapObject);
        }
        dataObject.put("mapData",mapArray);
        dataObject.put("valueName","总成交额");
        jsonObject.put("data",dataObject);
        return jsonObject;
    }

    //3：按各品类展示销售占比
    @RequestMapping("/findCatoTotal")
    public Object findCatoTotal(String nowDate){
        List<Map> list = gmallService.findCatoTotal("2024-04-21");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONArray dataArray = new JSONArray();
        for (Map map : list) {
            JSONObject mapObject = new JSONObject();
            mapObject.put("name",map.get("name"));
            mapObject.put("value",map.get("total"));
            dataArray.add(mapObject);
        }
        jsonObject.put("data",dataArray);
        return jsonObject;
    }
}
